package com.qf.shopping.controll.carControll;

import com.qf.shopping.pojo.car.CarMake;
import com.qf.shopping.pojo.car.CarModel;
import com.qf.shopping.service.carService.ICarMakeService;
import com.qf.shopping.service.carService.ICarModelService;
import com.qf.shopping.service.carService.impl.CarModelServiceImpl;
import com.qf.shopping.vo.carVO.CarModelConstom;
import com.qf.shopping.vo.carVO.CarModelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Humy
 * @date 2019/4/29 16:19
 */
@RequestMapping("/carModel")
@Controller
public class CarModelControll {

    @Autowired
    private ICarModelService carModelService;

    @Autowired
    private ICarMakeService carMakeService;

    /**
     * 加载所有车系
     */
    @RequestMapping("/getAllCarModels")
    public String getAllCarModels(Model model){

        List<CarModel> carModels = carModelService.queryAllCarModel();
        List<CarMake> carMakes = carMakeService.queryAll();
        //把所有类型放入共享域
        //1：微型车  2：小型 3：紧凑型车 4：中型车 5：中大型车 6：豪华车 7：SUV 8：MPV 9：跑车 10：概念车 11：面包车 12：皮卡 13：轻客
        //14：客车 15：卡车 16：轻卡 17：重卡'
        ArrayList<String> levelName = new ArrayList<>();
        levelName.add("微型车");
        levelName.add("小型车");
        levelName.add("紧凑型车");
        levelName.add("中型车");
        levelName.add("中大型车");
        levelName.add("豪华车");
        levelName.add("SUV");
        levelName.add("MPV");
        levelName.add("跑车");
        levelName.add("概念车");
        levelName.add("面包车");
        levelName.add("皮卡");
        levelName.add("轻客");
        levelName.add("客车");
        levelName.add("卡车");
        levelName.add("轻卡");
        levelName.add("重卡");

        model.addAttribute("carModels",carModels);
        model.addAttribute("carMakes",carMakes);
        model.addAttribute("levelNames",levelName);
        return "car/carModel/list";
    }

    /**
     * 根据id查询车系
     */
    @RequestMapping("/getCarModel")
    @ResponseBody
    public CarModel getCarModel(Long id){
        CarModel carModel = carModelService.queryCarModel(id);
        return carModel;
    }


    /**
     * 根据条件查询车系
     */
    @RequestMapping("getCarModels")
    public String getCarModels(CarModel carModel, Map<String,Object>map){
        List<CarModel> carModels = carModelService.query(carModel);
        map.put("carModels",carModels);

        return "car/carModel/list";
    }


    /**
     *新增车系
     */
    @PostMapping("/addCarModel")
    public String addCarModel(CarModel carModel,BindingResult bindingResult){
        carModelService.add(carModel);
        return "redirect:/carModel/getAllCarModels";
    }

    /**
     * 加载要修改的车系
     */
    @GetMapping("/updateCarModel")
    public CarModel updateCarModel(Long id){

        CarModel carModel = carModelService.queryCarModel(id);

        return carModel;
    }

    /**
     * 提交修改车系
     */
    @PostMapping("/updateCarModel")
    public String updateCarModel(CarModel carModel){
        carModelService.update(carModel);

        return "redirect:/carModel/getAllCarModels";
    }

    /**
     * 删除车系
     */
    @RequestMapping("/deleteCarModel/{id}")
    public String deleteCarModel(@PathVariable("id") Long...ids){

        carModelService.delete(ids);

        return "redirect:/carModel/getAllCarModels";
    }


}
